package com.chaoyi.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.ModelInfo;
import com.chaoyi.model.Role;
import com.chaoyi.model.Tree;
import com.chaoyi.service.ModelInfoService;
import com.chaoyi.service.RoleService;

@Controller
@RequestMapping("role")
public class RoleController {
	@Resource
	private RoleService roleService;
	@Resource
	private ModelInfoService modelInfoService;
	
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mod = new ModelAndView("/role/roleList");
		return mod;
	}
	
	@ResponseBody
	@RequestMapping("/roleList")
	public JSONObject roleList(HttpServletRequest request, HttpServletResponse response){
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<Role> roleList = roleService.findRoleList(offset,limit);
		JSONObject json = new JSONObject();
		json.put("rows", roleList);
		json.put("total", roleList.size());
		return json;
	}
	
	/**
	 * 新增跳转
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/addRole_plus")
	public ModelAndView addRole_plus(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mod = new ModelAndView("/role/addRole");
		return mod;
	}
	
	
	@ResponseBody
	@RequestMapping("/addRole")
	public JSONObject addClient(HttpServletRequest request, HttpServletResponse response,String roleName){
		JSONObject json = new JSONObject();
		int mark = roleService.addRole(roleName);
		if(mark==1){
			json.put("missage", "success");
		}
		return json;
	}
	
	@RequestMapping("/alertRole")
	public ModelAndView alertClient(HttpServletRequest request, HttpServletResponse response,int roleId){
		ModelAndView mod = new ModelAndView("role/alertRole");
		mod.addObject("obj",roleId);
		return mod;
	}
	
	@ResponseBody
	@RequestMapping("/findModelInfo")
	public Map<String,List<Tree>> findModelInfo(HttpServletRequest request, HttpServletResponse response ,int roleId){
		//该角色以有的权限
		List<Role> roleList = roleService.findModelInfo(roleId);
		//查询出所有的菜单
		List<ModelInfo> modelInfoList = modelInfoService.findAllModelInfo();
		//存放父节点
		List<ModelInfo> modelList = new ArrayList<ModelInfo>();
		//存放子节点
		List<ModelInfo> childList = new ArrayList<ModelInfo>();
		for(ModelInfo modelInfo : modelInfoList){
			if(modelInfo.getParentId()==0){
				modelList.add(modelInfo);
			}else{
				childList.add(modelInfo);
			}
		}
		List<Tree> treeList = new ArrayList<>();
		//设置父节点
		for(ModelInfo modelInfo : modelList){
            Tree parentTree = new Tree();
            parentTree.setId(modelInfo.getModelId());
            parentTree.setText(modelInfo.getModelName());
            treeList.add(parentTree);
		}
		//父节点默认不能选中
		for(Tree tree: treeList){
			tree.setState(tree.getParentMap(true));
		}
		for(Tree tree: treeList){
			List<Tree> childSaveList = new ArrayList<>();
			for(ModelInfo child: childList){
				//判断是否有子节点
				if(tree.getId()==child.getParentId()){
					//有子节点，设置父节点为可以选中
					tree.setState(tree.getParentMap(false));
					//将子节点绑定对应父节点上
					Tree childTree = new Tree();
					childTree.setId(child.getModelId());
					childTree.setText(child.getModelName());
					//判断用户角色菜单中是否存在此子菜单，如果存在状态为选择，不存在为不选中
					childTree.setState(childTree.getChildMap(false));
					for(Role role:roleList){
						if(role.getModelId()==child.getModelId()){
							childTree.setState(childTree.getChildMap(true));
						}
					}
					childSaveList.add(childTree);
				}
			}
			tree.setChildren(childSaveList);
		}
        Map<String,List<Tree>> resultMap = new HashMap<>();
        resultMap.put("rows",treeList);
        return resultMap;
	}
	
	//保存树
	@ResponseBody
	@RequestMapping("/updateModelInfo")
	public JSONObject updateModelInfo(int roleId,String menuInfos){
        List<String> menuInfoList = new ArrayList<>();
        if(menuInfos !=null&&!menuInfos.isEmpty()){
            menuInfoList = Arrays.asList(menuInfos.split(","));
        }
		JSONObject json = new JSONObject();
		int numbers = roleService.updateModelInfo(roleId,menuInfoList);
		if(numbers !=0){
			json.put("missage", "success");
			return json;
		}
		json.put("missage", "error");
		return json;
	}
	
	
	//删除保存
	@ResponseBody
	@RequestMapping("/deleteRole")
	public JSONObject deleteRole(HttpServletRequest request, HttpServletResponse response,int id){
		JSONObject json = new JSONObject();
		int numbers = roleService.deleteRole(id);
		if(numbers !=0){
			json.put("missage", "success");
			return json;
		}
		json.put("missage", "error");
		return json;
	}
	
	//角色下拉菜单
	@ResponseBody
	@RequestMapping("/findRoleName")
	public JSONObject findRoleName(HttpServletRequest request, HttpServletResponse response){
		JSONObject json = new JSONObject();
		List<Role> roleList = roleService.findRoleName();
		json.put("roleList", roleList);
		return json;
	}
}
